/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.sourceprojects.lycia.TypeConverter;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.annotations.ContextObject;
import org.sourceprojects.lycia.exceptions.LyciaInitializationException;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;

public class StaticContextTestCase {
	private static final String XML_DATA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<synset id=\"matcher\" />";

	@Test
	public void testStaticInnerDataConverter() throws LyciaParsingException {
		FluentBuilder<StringBuilder> builder = FluentBuilder.prepare();
		LyciaParser<StringBuilder> parser = builder.parser(
				FluentBuilder.tag("*", FluentBuilder.method("resolveId", this,
						String.class, StringBuilder.class))).build();

		StringBuilder sb = new StringBuilder();
		parser.getParserContext().setContextObject(sb);
		parser.parse(XML_DATA);

		assertEquals("matcher", sb.toString());
	}

	@Test(expected = LyciaInitializationException.class)
	public void testStaticMethodRegistrationFluentBuilder() {
		FluentBuilder<StringBuilder> builder = FluentBuilder.prepare();
		builder.parser(
				FluentBuilder.tag("*", FluentBuilder.method("resolveId",
						getClass(), String.class))).build();
	}

	@SuppressWarnings("unused")
	private static final void resolveId(@Attribute("id") String id) {
		return;
	}

	@SuppressWarnings("unused")
	private final void resolveId(
			@Attribute(value = "id", converter = StaticInnerDataConverter.class) String id,
			@ContextObject StringBuilder sb) {

		sb.append(id);
	}

	private static class StaticInnerDataConverter implements TypeConverter {

		@Override
		public boolean accept(Class<?> type) {
			return true;
		}

		@Override
		public Object convert(Object value, Class<?> type) throws Exception {
			return value;
		}

		@Override
		public <B> Object convert(Object value, Class<?> type,
				LyciaParser<B> parser) throws Exception {

			return value;
		}

	}

}
